package com.atwork.wuhua.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.TypeSelectRegionAdapter;
import com.atwork.wuhua.base.BaseReq;
import com.atwork.wuhua.bean.EvaluateBean;
import com.atwork.wuhua.bean.LevelBean;
import com.atwork.wuhua.bean.TypeSelectRegionBean;
import com.atwork.wuhua.dialog.PopLevel;
import com.atwork.wuhua.dialog.PopRegion;
import com.atwork.wuhua.mvp.Callback;
import com.atwork.wuhua.mvp.DataModel;
import com.atwork.wuhua.mvp.Token;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TypeSelectUtil implements View.OnClickListener {
    private LinearLayout llyRegion, llyLevel, llyDistance;
    private TextView tvRegion;
    private ImageView imgRegionType, imgLevelTop, imgLevelBoom, imgDistanceTop, imgDistanceBoom;

    private List<TypeSelectRegionBean.DataBean> typeSelectRegionBeans;
    private List<LevelBean> levelBeans;
    private Context context;
    private OnSelectResultLinstener onSelectResultLinstener;

    public interface OnSelectResultLinstener {
        void onselect(int type, String code);
    }

    public final static int REGIN = 1;
    public final static int LEVEL = 2;
    public final static int DISTANCE = 3;
    private int starCode = 1; // 星级排序 1 离最近的时间 2 最远的时间
    private int distanceCode = 1; // 时间排序 1 离最近的时间 2 最远的时间

    private String sort = "ASC";

    public TypeSelectUtil(Context context, LinearLayout llyRegion, ImageView imgRegionType, TextView tvRegion,
                          LinearLayout llyLevel, ImageView imgLevelTop, ImageView imgLevelBoom,
                          LinearLayout llyDistance, ImageView imgDistanceTop, ImageView imgDistanceBoom) {
        this.context = context;
        this.tvRegion = tvRegion;
        this.llyRegion = llyRegion;
        this.llyLevel = llyLevel;
        this.imgRegionType = imgRegionType;
        this.imgLevelTop = imgLevelTop;
        this.imgLevelBoom = imgLevelBoom;
        this.llyDistance = llyDistance;
        this.imgDistanceTop = imgDistanceTop;
        this.imgDistanceBoom = imgDistanceBoom;

        llyRegion.setOnClickListener(this);
        llyLevel.setOnClickListener(this);
        llyDistance.setOnClickListener(this);
        addReginData();
        addLevelData();

    }

    public void setOnSelectResult(OnSelectResultLinstener onSelectResult) {
        this.onSelectResultLinstener = onSelectResult;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lly_region:
                initRegionPop(llyRegion);
                break;

            case R.id.lly_level:
//                initLevelPop(llyRegion);

                if (starCode == 1) {
                    starCode = 2;
                    sort = "DESC";
                    imgLevelTop.setBackgroundResource(R.mipmap.icon_triangle_top_noselect);
                    imgLevelBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_select);
                } else {
                    starCode = 1;
                    sort = "ASC";
                    imgLevelTop.setBackgroundResource(R.mipmap.icon_triangle_top_select);
                    imgLevelBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                }
                imgDistanceTop.setBackgroundResource(R.mipmap.icon_triangle_top_noselect);
                imgDistanceBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                onSelectResultLinstener.onselect(LEVEL, sort);
                break;

            case R.id.lly_distance:
                if (distanceCode == 1) {
                    distanceCode = 2;
                    sort = "DESC";
                    imgDistanceTop.setBackgroundResource(R.mipmap.icon_triangle_top_noselect);
                    imgDistanceBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_select);
                } else {
                    distanceCode = 1;
                    sort = "ASC";
                    imgDistanceTop.setBackgroundResource(R.mipmap.icon_triangle_top_select);
                    imgDistanceBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                }

                imgLevelTop.setBackgroundResource(R.mipmap.icon_triangle_top_noselect);
                imgLevelBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                onSelectResultLinstener.onselect(DISTANCE, sort);
                break;
        }
    }

    private PopRegion popRegion;

    private void initRegionPop(View view) {
        imgRegionType.setBackgroundResource(R.mipmap.icon_triangle_top_select);
        popRegion = new PopRegion(context, typeSelectRegionBeans, new PopRegion.OnSelectLinstener() {
            @Override
            public void onselect(int positon) {
                tvRegion.setText(typeSelectRegionBeans.get(positon).getName());
                onSelectResultLinstener.onselect(REGIN, typeSelectRegionBeans.get(positon).getCode());
                popRegion.dismiss();
            }
        });
        popRegion.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                imgRegionType.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
            }
        });
        popRegion.showAsDropDown(view);
    }

    private PopLevel popLevel;

    private void initLevelPop(View view) {
        popLevel = new PopLevel(context, levelBeans, new PopLevel.OnSelectLinstener() {
            @Override
            public void onselect(int positon) {
                onSelectResultLinstener.onselect(LEVEL, levelBeans.get(positon).getCode());
                popLevel.dismiss();
            }
        });
        popLevel.showAsDropDown(view);
    }

    private void addLevelData() {
        levelBeans = new ArrayList<>();
        levelBeans.add(new LevelBean("0", "不限星级"));
        levelBeans.add(new LevelBean("5", "五星"));
        levelBeans.add(new LevelBean("4", "四星"));
        levelBeans.add(new LevelBean("3", "三星"));
        levelBeans.add(new LevelBean("2", "二星"));
        levelBeans.add(new LevelBean("1", "一星"));
        levelBeans.get(0).setSelect(true);
    }

    private void addReginData() {
        typeSelectRegionBeans = new ArrayList<>();
        BaseReq req = new BaseReq();
        req.setKey("type", "area");
        req.setKey("parent", "3502");
        DataModel.request(Token.API_LOCATION)
                .params(req)
                .execute(new Callback<TypeSelectRegionBean>() {

                    public void onSuccess(String msg, TypeSelectRegionBean data) {
                        typeSelectRegionBeans.clear();
                        typeSelectRegionBeans.add(new TypeSelectRegionBean.DataBean("0", "不限区域"));
                        typeSelectRegionBeans.addAll(data.getData());
                        typeSelectRegionBeans.get(0).setSelect(true);
                    }

                    @Override
                    public void onFailure(String msg) {
                    }

                    @Override
                    public void onError() {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
